<?php
	
	require_once(ROOTDIR.'source/service/class/class.main.php');
	
	class _Tongji_log extends Main
	{
	
		private static $tongji_log = 'tongji_log';
		
		private static function chuli($object,$sql)
		{
			fileLog($sql);
			$variable = $object->query($sql);
			
			if(!empty($variable[0]['count']))
			{
				return $variable[0]['count'];
			}
			else
			{
				return intval($variable[0]['count']);
			}

			
		}
		
		public static function getUserbrowseListByChanpinID($uid)
		{
			$sql = sprintf("select chanpin.name,chanpin.id from tongji_log,chanpin where chanpin.id = tongji_log.pid and uid='%s' and chanpin.status='2' and type='item' GROUP BY pid ORDER BY addtime desc",$uid);
			fileLog($sql);
			return self::getMysqlInstance()->query($sql);
		}
		
		public static function getUserbrowseListByItemID($uid,$pid)
		{
			$sql = sprintf("select chanpin_item.name,chanpin_item.id from tongji_log,chanpin_item where chanpin_item.id = tongji_log.iid and uid='%s' and tongji_log.pid='%s' and chanpin_item.status='1' and tongji_log.type='item' GROUP BY iid ORDER BY addtime desc",$uid,$pid);
			fileLog($sql);
			return self::getMysqlInstance()->query($sql);
		}
		
		
		
		public static function getChanpinItem_tongji($wheres)
		{
			//浏览PV
			$sql_pv = sprintf("select count(1) as count from tongji_log where type='item' and iid='%s' and `time` between %s and %s",$wheres['iid'],$wheres['start'],$wheres['end']);
			$result['pv'] =  self::chuli(self::getMysqlInstance(),$sql_pv);
			//浏览UV
			$sql_uv = sprintf("select count(1) as count from (select DISTINCT cookie from tongji_log where type='item' and iid='%s' and `time` between %s and %s) as t",$wheres['iid'],$wheres['start'],$wheres['end']);
			$result['uv'] =  self::chuli(self::getMysqlInstance(),$sql_uv);
			
			//浏览IP
			$sql_ip = sprintf("select count(1) as count from  (select DISTINCT ip from tongji_log where type='item' and iid='%s' and `time` between %s and %s) as t",$wheres['iid'],$wheres['start'],$wheres['end']);
			$result['ip'] =  self::chuli(self::getMysqlInstance(),$sql_ip);
			
			//用户投资金额	
			$sql_touzi = sprintf("select SUM(money) as count from fanli where iid='%s' and status='2' and `addtime` between %s and %s",$wheres['iid'],$wheres['start'],$wheres['end']);
			$result['touzi'] =  self::chuli(self::getMysqlInstance(),$sql_touzi);
			
			//用户返利金额
			$sql_fanli = sprintf("select SUM(fanli) as count from fanli where iid='%s' and status='2' and `addtime` between %s and %s",$wheres['iid'],$wheres['start'],$wheres['end']);
			$result['fanli'] =  self::chuli(self::getMysqlInstance(),$sql_fanli);
			
			return $result;
		}
		
		public static function getChanpin_tongji($wheres)
		{
			//浏览PV
			$sql_pv = sprintf("select count(1) as count from tongji_log where type='chanpin' and pid='%s' and `time` between %s and %s",$wheres['pid'],$wheres['start'],$wheres['end']);
			$result['pv'] =  self::chuli(self::getMysqlInstance(),$sql_pv);
			//浏览UV
			$sql_uv = sprintf("select count(1) as count from (select DISTINCT cookie from tongji_log where type='chanpin' and pid='%s' and `time` between %s and %s) as t",$wheres['pid'],$wheres['start'],$wheres['end']);
			$result['uv'] =  self::chuli(self::getMysqlInstance(),$sql_uv);
			
			//浏览IP
			$sql_ip = sprintf("select count(1) as count from  (select DISTINCT ip from tongji_log where type='chanpin' and pid='%s' and `time` between %s and %s) as t",$wheres['pid'],$wheres['start'],$wheres['end']);
			$result['ip'] =  self::chuli(self::getMysqlInstance(),$sql_ip);
			
			//用户投资金额	
			$sql_touzi = sprintf("select SUM(money) as count from fanli where pid='%s' and status='2' and `addtime` between %s and %s",$wheres['pid'],$wheres['start'],$wheres['end']);
			$result['touzi'] =  self::chuli(self::getMysqlInstance(),$sql_touzi);
			
			//用户返利金额
			$sql_fanli = sprintf("select SUM(fanli) as count from fanli where pid='%s' and status='2' and `addtime` between %s and %s",$wheres['pid'],$wheres['start'],$wheres['end']);
			$result['fanli'] =  self::chuli(self::getMysqlInstance(),$sql_fanli);
			
			
			return $result;
		}
		
		
		
		
		
		
		
		
		public static function addTongji_logInfo($data)
		{
			$set = join(',',self::setMySQLParam($data));
			if(!empty($set))
			{
				$sql = sprintf("insert into %s set %s ",self::$tongji_log,$set);
				fileLog($sql);
				return self::getMysqlInstance()->insert_sql($sql);
			}
			else
			{
				return false;
			}
		}
		
		public static function setTongji_logInfo($where,$data)
		{
			$set = join(',',self::setMySQLParam($data));
			if(!empty($set) && !empty($where['id']))
			{
				$sql = sprintf("update %s set %s where id='%s'",self::$tongji_log,$set,$where['id']);
				fileLog($sql);
				return self::getMysqlInstance()->diy_update_sql($sql);
			}
			else
			{
				return false;
			}
		}
		
		public static function getTongji_logInfo($wheres)
		{
			$where = join(' and ',self::setMySQLParam($wheres));
			$sql = sprintf("select * from %s where %s",self::$tongji_log,$where);
			fileLog($sql);
			$res = self::getMysqlInstance()->query($sql);
			
			return $res[0];
		}
		
		public static function getTongji_logList($wheres,$page,$between,$sort)
		{
			$where = join(' and ',self::setMySQLParam($wheres));
			
			if(empty($where))
			{
				$where = ' 1=1 ';
			}
			
			if(isset($between['start']) && isset($between['end']))
			{
				$where .= ' and `addtime` between "'.$between['start'].'" and "'.$between['end'].'" ';
			}
			
			if(!empty($page))
			{
				$sqlCount = "select count(id) as count from tongji_log where ".$where;
			
				fileLog($sqlCount,'debug');
			
				$resCount = self::getMysqlInstance()->query($sqlCount);
				
				if(!empty($resCount[0]))
				{
					$pagenum=ceil($resCount[0]['count']/$page['rows']); 
				}
				else
				{
					$pagenum=ceil(0/$page['rows']); 
				}
				
				$offset=($page['page']-1)*$page['rows'];   
				$limit = " limit $offset,".$page['rows'];
			}
			$select = 'select * ';
			if(is_array($sort) && $sort)
			{
				$order =' order by ';
				foreach($sort as $k => $v)
				{
					$ordertemp[] =" `{$k}` ".self::getMysqlInstance()->strescape($v);
				}
				
				$order .= implode(',', $ordertemp);
			}
			$sql = sprintf("%s from %s where %s %s %s",$select,self::$tongji_log,$where,$order,$limit);
			fileLog($sql);
			if(!empty($page))
			{
				$result['list'] = self::getMysqlInstance()->query($sql);
				
				$result['pages'] = $pagenum;
			}
			else
			{
				$result =  self::getMysqlInstance()->query($sql);
			}
			return $result;
		}
		
	}